<template>
    <goods-sku :localdata="goodsInfo" @addCartEv="addCart"></goods-sku>
</template>
<script>
import GoodsSku from "@/components/GoodsSku.vue";
import {
    addCartApi
} from '../../../api/goods';
console.log('addCartApi', addCartApi)
export default {
    components: {
        GoodsSku,
    },
    props: {
        // 商品详情信息
        goods: {
            type: Object,
            default: {},
        },
    },
    data() {
        return {
            goodsInfo: {},
        };
    },

    created() {
        const app = this;
        const { goods } = app;
        app.goodsInfo = {
            _id: goods.goods_id,
            name: goods.goods_name,
            goods_thumb: goods.images,
            goods_price: goods.goods_price_min,
            market_price: goods.market_price_min,
            collect_flag: goods.collect_flag,
            goods_unit:goods.goods_unit,
            producer:goods.producer,
            sku_list: app.getSkuList(),
            spec_list: app.getSpecList(),
            spec_type:goods.spec_type
        };
    },
    methods: {
        // 整理商品SKU列表
        getSkuList() {
            const app = this;
            const {
                goods: { goods_name, goods_image, skuList },
            } = app;
            const skuData = [];
            skuList.forEach((item) => {
                skuData.push({
                    _id: item.id,
                    goods_sku_id: item.goods_sku_id,
                    goods_id: item.goods_id,
                    goods_name: goods_name,
                    image: item.image ? item.image : goods_image,
                    // price: item.goods_price * 100,
                    price: item.goods_price,
                    market_price: item.market_price,
                    stock: item.stock_num,
                    unit:item.goods_unit,
                    minimum: item.minimum,
                    spec_value_ids: item.spec_value_ids,
                    sku_name_arr: this.getSkuNameArr(item.spec_value_ids)
                });
            });
            return skuData;
        },

        // 获取sku记录的规格值列表
        getSkuNameArr(specValueIds) {
            const app = this;
            const defaultData = ["默认"];
            const skuNameArr = [];
            if (specValueIds) {
                specValueIds.forEach((valueId, groupIndex) => {
                    const specValueName = app.getSpecValueName(
                        valueId,
                        groupIndex
                    );
                    skuNameArr.push(specValueName);
                });
            }
            return skuNameArr.length ? skuNameArr : defaultData;
        },

        // 获取指定的规格值名称
        getSpecValueName(valueId, groupIndex) {
            const app = this;
            const {
                goods: { specList },
            } = app;
            const res = specList[groupIndex].valueList.find((specValue) => {
                return specValue.spec_value_id == valueId;
            });
            return res.spec_value;
        },

        // 整理规格数据
        getSpecList() {
            const {
                goods: { specList },
            } = this;
            const defaultData = [
                {
                    name: "默认",
                    list: [
                        {
                            name: "默认",
                        },
                    ],
                },
            ];
            if (this.goods.spec_type == 10) {
                return defaultData;
            } else {
                const specData = []
                specList.forEach(group => {
                    const children = []
                    group.valueList.forEach(specValue => {
                        children.push({
                            name: specValue.spec_value
                        })
                    })
                    specData.push({
                        name: group.spec_name,
                        list: children
                    })
                })
                return specData.length ? specData : defaultData
            }
        },
        // 加入购物车按钮
        addCart(selectShop) {
            const that = this;
            const { goods_id, goods_sku_id, buy_num } = selectShop;
            addCartApi({
                goodsId: goods_id,
                goodNum: buy_num,
                goodsSkuId: goods_sku_id,
            }).then(res => {
                if (res.code == 0) {
                    that.$message.success(res.msg);
                    that.$emit('toUpdateCartNum')
                } else {
                    if (res.code == 101) {
                        that.$router.push({
                            path: '/login',
                            query: {
                                redirect: `/goods_detail?goods_id=${that.goodsInfo._id}`
                            }
                        })
                        return;
                    }
                    if (res.code == 10024) {
                        // that.$router.push({
                        //     path: '/user/enterprise_auth',
                        //     query: {
                        //         redirect: `/goods_detail?goods_id=${that.goodsInfo._id}`
                        //     } 
                        // })
                        setTimeout(() => {
                            that.$router.push('/user/enterprise_auth')
                        }, 2000)
                        return;
                    }
                }
            })
        },
    },
};
</script>
<style lang="scss" scoped>
</style>